﻿function botonesEdicion(tipo) {
    $.jgrid.nav.addtext = "Agregar";
    $.jgrid.nav.edittext = "Editar";
    $.jgrid.nav.deltext = "Eliminar";
    $.jgrid.nav.viewtext = "Vista";
    $.jgrid.edit.addCaption = "<span style='font-size:13px'>Agregar " + tipo + "</span>";
    $.jgrid.edit.editCaption = "<span style='font-size:13px'>Editar " + tipo + "</span>";
    $.jgrid.view.caption = "<span style='font-size:13px'>Ver " + tipo + "</span>";
    $.jgrid.del.caption = "<span style='font-size:13px'>Eliminar " + tipo + "</span>";
    $.jgrid.del.msg = "Desea eliminar "+ tipo +" seleccionado?";
};

function ventana(id, width, height, modal, position) {
    $(id).dialog({
        autoOpen: false,
        modal: modal,
        position: position,
        width: width,
        height: height,
        show: {
            effect: "drop",
            duration: 500
        },
        buttons: {
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });
}

function fecha(id, oper) {

    if (oper == "Factura") {
        $(id).datepicker();
        $(id).datepicker('option', { dateFormat: 'dd/mm/yy', minDate: 0 });
    }

    else {
        $(id).datepicker();
        $(id).datepicker('option', { dateFormat: 'dd/mm/yy' });
    }

}

function actualizarGrid(url, grid) { // Recibimos archivo a actualizar
    var urlRecargar = url;
    $(grid).jqGrid('setGridParam', { url: urlRecargar, datatype: "json" });
    $(grid).trigger("reloadGrid");
}

/* ====================================================================================================================================
    PLATOS
====================================================================================================================================*/

function Platos(url, edit) {
    botonesEdicion("Platos");
    $("#list_compras").jqGrid({
        url: url,
        datatype: 'json',
        mtype: 'POST',
        height: 250,
        colNames: ['Id Producto', 'Descripcion', 'Estado', 'Precio', 'Id Categoria'],
        colModel: [
            { name: 'idproducto', index: 'idproducto', width: 55, hidden:true ,editable: false, editoptions: { readonly: true, size: 10 } },
            { name: 'descripcion', index: 'descripcion', align: 'center', width: 60, editable: true, editoptions: { maxlength: 50, size: 35 },editrules: {required: true}, 
              formoptions: {label: 'Descripción: *'} },
            { name: 'estado', index: 'estado', width: 20, align: 'center', editable: true, formatter: 'checkbox', edittype: "checkbox", editoptions: { value: "True:False", defaultValue: "True" } },
            { name: 'precio', index: 'precio', editable: true, width: 30, align: 'center', editoptions: { maxlength: 8, size: 10 }, editrules: {required: true}, 
              formoptions: {label: 'Precio: *'} },
            { name: 'idcategoria', index: 'idcategoria', width: 50, align: 'left', editable: true, hidden: false, editrules: {required: true}, 
              formoptions: {label: 'Id Categoria*'} }
        ],        
        rowNum: 10,
        rowList: [10, 20, 50],
        pager: '#pager_com',              
        sortname: 'idproducto',
        viewrecords: true,
        sortorder: 'desc',
        caption: '<span style="font-size:13px">Platos</span>',
        editurl: "InUp_Platos", height: 210
    }
    );

    jQuery("#list_compras").jqGrid('navGrid', '#pager_com', { view: false, del:false, search:false }, //options
        
        { jqModal: true, checkOnUpdate: true, savekey: [true, 13], navkeys: [true, 38, 40], checkOnSubmit: true, reloadAfterSubmit: false, closeOnEscape: true, bottominfo: "Las cajas con (*) son requeridas" }, // edit options 
        { jqModal: true, checkOnUpdate: true, savekey: [true, 13], navkeys: [true, 38, 40], checkOnSubmit: true, reloadAfterSubmit: false, closeOnEscape: true,  width:450, bottominfo: "Las cajas con (*) son requeridas" }, // add options 
        //{ reloadAfterSubmit: false, jqModal: false, closeOnEscape: true }, // del options 
        { closeOnEscape: true }, // search options 
        { navkeys: [true, 38, 40], height: 270, jqModal: false, closeOnEscape: true } // view options
        );
}

/* ====================================================================================================================================
    DETALLE PEDIDO
====================================================================================================================================*/

function DetallePedido(edit) {
    var idcliente = $("#idcliente").val();// obtengo el id de cliente
    var idmesa = $("#idmesa").val();
    botonesEdicion("Detalle Pedido");
    $("#calcular").attr('disabled', false);
    $("#list_detalle_pedido").jqGrid({
        url: "/Comprobante/listados?tipo=2&idmesa=" + idmesa + "&idcliente=" + idcliente + "",
        //url: "@Url.Action('listados', 'Comprobante')" +"?tipo=2&idmesa="+idmesa+"&idcliente="+idcliente+"",
        datatype: 'json',
        mtype: 'POST',
        height: 200,
        colNames: ['Id Pedido', 'Id Mesa', 'N# Mesa', 'Id Cliente', "Id Mozo", "Plato", "Cantidad", "Precio", "Importe"],
        colModel: [
                    { name: 'idcabecerapedido', index: 'idcabecerapedido', hidden: false, editable: false, width: 70, align: 'center' },
                    { name: 'idmesa', index: 'idmesa', width: 90, editable: false, hidden: true },
                    { name: 'mesa', index: 'mesa', width: 100, editable: false, editoptions: { maxlength: 50, size: 35 }, align: 'center' },
                    { name: 'idcliente', index: 'idcliente', width: 90, editable: false, hidden: true },
                    //{ name: 'cliente', index: 'cliente', width: 100, editable: false, editoptions: { maxlength: 50, size: 35 }, align: 'center' },
                    { name: 'idempleado', index: 'idempleado', width: 90, editable: false, hidden: true },
                    //{ name: 'cantidad', index: 'cantidad', editable: true, width: 80, align: 'center', editoptions: { maxlength: 8, size: 10 } },
                    { name: 'plato', index: 'plato', width: 100, editable: false, editoptions: { maxlength: 50, size: 35 }, align: 'center' },
                    { name: 'precio', index: 'precio', width: 100, editable: false, editrules: { number: true } },
                    { name: 'cantidad', index: 'cantidad', width: 100, editable: false, editrules: { number: true } },

                    { name: 'importe', index: 'importe', width: 100 }

        ],
        pager: '#pager_detalle_pedido',
        rowNum: 10,
        autowidth: true,
        gridview: true,
        rownumbers: true,
        rowList: [15, 25, 50],
        sortname: 'idcabecerapedido',
        sortorder: "desc",
        viewrecords: true,
        //imgpath: '/scripts/themes/coffee/images',
        caption: '<span style="font-size:13px">Detalle Pedido</span>'     
        , height: 80 //width: 366        
        , forceFit: true
        , cellEdit: true
        , cellsubmit: 'clientArray',
        afterSaveCell: function (rowid, name, val, iRow, iCol) {
            if (name == 'precio') {
                var cantval = $("#list_detalle_pedido").jqGrid('getCell', rowid, iCol + 1);
                $("#list_detalle_pedido").jqGrid('setRowData', rowid, { importe: parseFloat(val) * parseFloat(cantval) });
            }
            if (name == 'cantidad') {
                var preval = $("#list_detalle_pedido").jqGrid('getCell', rowid, iCol - 1);
                $("#list_detalle_pedido").jqGrid('setRowData', rowid, { importe: parseFloat(val) * parseFloat(preval) });
            }
        }
    }).navGrid('#pager_detalle_pedido', { edit: edit, add: edit, del: false, search: false, refresh: true });
}

/* ====================================================================================================================================
    PEDIDO
====================================================================================================================================*/

function Pedido(url, edit) {

    var updateDialog = {
        url: url
                , closeAfterAdd: true
                , closeAfterEdit: true
                , modal: true
                , onclickSubmit: function (params) {
                    var ajaxData = {};

                    var list = $("#list_pedido");
                    var selectedRow = list.getGridParam("selrow");
                    rowData = list.getRowData(selectedRow);
                    ajaxData = { ContactId: rowData.ContactId };

                    return ajaxData;
                }

                , width: "400"
    };

    botonesEdicion("Pedido");
    $("#list_pedido").jqGrid({
        url: url,
        datatype: 'json',
        mtype: 'POST',
        height: 300,
        colNames: ['Id Pedido', 'Id Mesa', 'N# Mesa', 'Id Cliente', 'Cliente', "Id Mozo", "ruc"],
        colModel: [
                    { name: 'idcabecerapedido', index: 'idcabecerapedido', hidden: false, editable: true, width: 70, align: 'center' },
                    { name: 'idmesa', index: 'idmesa', width: 90, editable: true, hidden: true },
                    { name: 'mesa', index: 'mesa', width: 100, editable: true, editoptions: { maxlength: 50, size: 35 }, align: 'center' },
                    { name: 'idcliente', index: 'idcliente', width: 90, editable: true, hidden: true },
                    { name: 'cliente', index: 'cliente', width: 100, editable: true, editoptions: { maxlength: 50, size: 35 }, align: 'center' },
                    { name: 'idmozo', index: 'idmozo', width: 90, editable: true, hidden: true },
                    { name: 'ruc', index: 'ruc', width: 70, editable: true, editoptions: { maxlength: 50, size: 35 }, align: 'center' }
        ],
        pager: '#pager_pedido',
        rowNum: 10,        
        rowList: [15, 25, 50],
        loadonce: true,
        autowidth: true,
        rownumbers: true,
        gridview: true,
        sortname: 'idcabecerapedido',
        sortorder: "asc",
        viewrecords: true,        
        caption: '<span style="font-size:13px">Pedido</span>',
        ondblClickRow: function (rowid, iRow, iCol, e) {//usarlo solo en el caso q se quiera hacer doble click sobre una fila
            if (edit == true) {
                $("#list_pedido").editGridRow(rowid, updateDialog);
            } else {
                var i = $(this).getRowData(rowid);
                $('#idcabecerapedido').val(i.idcabecerapedido);
                $('#idmesa').val(i.idmesa);
                $('#mesa').val(i.mesa);
                $('#idcliente').val(i.idcliente);
                $('#cliente').val(i.cliente);
                $('#idmozo').val(i.idmozo);
                $('#ruc').val(i.ruc);
                $("#cliente").attr('disabled', true);
                $("#ruc").attr('disabled', true);
                $("#mesa").attr('disabled', true);

                $('#dialogPedido').dialog('close');
                if ($('#idcabecerapedido').val(i.idcabecerapedido) == 1)
                    console.log("es verdad");                
            }
        }
    });

    jQuery("#list_pedido").jqGrid('navGrid', '#pager_pedido', { del: false, add: false, edit: false, search: false });
    jQuery("#list_pedido").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false });

}